package com.autolinq.service;

import java.text.SimpleDateFormat;
import java.util.Date;

import net.sf.json.JSONObject;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;

import com.autolinq.pojo.WeChatMessage;
import com.autolinq.util.http.HttpPostMessage;

public class WechatDeviceService extends AbstractWechatService{

	@Override
	public String handleWechatMessage(WeChatMessage message,
			String qrcodePath) {
		String contentDec = getFromBASE64(message.getContent());
		JSONObject jo = JSONObject.fromObject(contentDec);
		
		String funcID = (String)jo.get("FuncID");
		
		String json = "{\"Source\":\"MRN\"}";
		JSONObject jsonObj = JSONObject.fromObject(json);
		jsonObj.put("FunctionID", funcID);
		jsonObj.put("OpenID", message.getOpenID());
		jsonObj.put("DeviceID", message.getDeviceID());
		jsonObj.put("Message", jo);
		
		System.out.println("funcID--"+funcID+":"+jsonObj.toString());
		
		String contentRes = "";
		if("Init".equals(funcID)){
			contentRes = HttpPostMessage.postMessage(prop.getProperty("otaInitURL"), jsonObj.toString());
		}else if("InitCheck".equals(funcID)){
			contentRes = HttpPostMessage.postMessage(prop.getProperty("otaInitCheckURL"), jsonObj.toString());
		}else if("GetActiveTripInfo".equals(funcID)){
			contentRes = HttpPostMessage.postMessage(prop.getProperty("otaGetActiveTripInfoURL"), jsonObj.toString());
		}else if("RetrieveDynamicInfo".equals(funcID)){
			contentRes = HttpPostMessage.postMessage(prop.getProperty("otaRetrieveDynamicInfoURL"), jsonObj.toString());
		}else if("TerminateCoDriveTrip".equals(funcID)){
			contentRes = HttpPostMessage.postMessage(prop.getProperty("otaTerminateCoDriveTripURL"), jsonObj.toString());
		}else if("TerminateCoDriveTrip".equals(funcID)){
			contentRes = HttpPostMessage.postMessage(prop.getProperty("otaTerminateCoDriveTripURL"), jsonObj.toString());
		}else if("CoDriveTripAck".equals(funcID)){
			contentRes = HttpPostMessage.postMessage(prop.getProperty("otaCoDriveTripAckURL"), jsonObj.toString());
		}else if("HeartPulse".equals(funcID)){
			contentRes = HttpPostMessage.postMessage(prop.getProperty("otaHeartPulseURL"), jsonObj.toString());
		}else if("PickUpReq".equals(funcID)){
			contentRes = HttpPostMessage.postMessage(prop.getProperty("otaPickUpReqURL"), jsonObj.toString());
		}else if("RetrievePickUpStatus".equals(funcID)){
			contentRes = HttpPostMessage.postMessage(prop.getProperty("otaRetrievePickUpStatusURL"), jsonObj.toString());
		}else if("TerminalPickUp".equals(funcID)){
			contentRes = HttpPostMessage.postMessage(prop.getProperty("otaTerminalPickUpURL"), jsonObj.toString());
		}else if("GetProfiles".equals(funcID)){
			contentRes = HttpPostMessage.postMessage(prop.getProperty("otaGetProfilesURL"), jsonObj.toString());
		}else if("GetPortraits".equals(funcID)){
			contentRes = HttpPostMessage.postMessage(prop.getProperty("otaGetPortraitsURL"), jsonObj.toString());
		}else if("SendGroupMsg".equals(funcID)){
			contentRes = HttpPostMessage.postMessage(prop.getProperty("otaSendGroupMsgURL"), jsonObj.toString());
		}else if("GroupMsgReq".equals(funcID)){
			contentRes = HttpPostMessage.postMessage(prop.getProperty("otaGroupMsgReqMsgURL"), jsonObj.toString());
		}
		
		String returnStr = "";

		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");

		String times = format.format(new Date());
		String timeLong = String.valueOf(new Date().getTime());
		times = timeLong.substring(0, timeLong.length()-3);

		Element rootXML = new Element("xml");

		rootXML.addContent(new Element("ToUserName").setText(message.getFromUserName()));
		rootXML.addContent(new Element("FromUserName").setText(message.getToUserName()));
		rootXML.addContent(new Element("CreateTime").setText(times));
		rootXML.addContent(new Element("MsgType").setText("device_text"));	
		rootXML.addContent(new Element("DeviceType").setText(message.getDeviceType()));
		rootXML.addContent(new Element("DeviceID").setText(message.getDeviceID()));
		//rootXML.addContent(new Element("DeviceID").setText("92403923885")); 
		rootXML.addContent(new Element("SessionID").setText(message.getSessionID()));
		String contentBase64 = getBASE64(contentRes);
		rootXML.addContent(new Element("Content").setText(contentBase64));	

		
		Document doc = new Document(rootXML);

		XMLOutputter XMLOut = new XMLOutputter();

		returnStr = XMLOut.outputString(doc);
		
		if(returnStr!=null && returnStr.length()>38){
			returnStr = returnStr.substring(38);
		}

		return returnStr;
	}
	
}